//
//  XHWithdrawalSubsidiaryViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHWithdrawalSubsidiaryViewController.h"

#import "XHSubsidiaryCell.h"

#import "Services.h"

#import <SVPullToRefresh/SVPullToRefresh.h>
#import <SDWebImage/UIImageView+WebCache.h>

static const NSUInteger kPageSize = 20;
static const NSUInteger kPageInit = 0;

@interface XHWithdrawalSubsidiaryViewController ()

@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSMutableArray *m_messagesArray;
@property (assign, nonatomic) NSUInteger page;
@property (assign, nonatomic) BOOL hasNextPage;
@end

@implementation XHWithdrawalSubsidiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHSubsidiaryCell" bundle:nil] forCellReuseIdentifier:XHSubsidiaryCellIdentifier];
    self.page=kPageInit;
    [self.ui_tableView addInfiniteScrollingWithActionHandler:^{
        if (!self.hasNextPage)
        {
        }
        if (self.m_messagesArray.count>=kPageSize){
            [self getWithdrawRecord:self.page+1];
            return;
        }
        [self.ui_tableView.infiniteScrollingView stopAnimating];
    }];
    [self.ui_tableView addPullToRefreshWithActionHandler:^{
        self.page=kPageInit;
        [self getWithdrawRecord:self.page];
    }];
    [PublicMethod setPullToRefreshView:self.ui_tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.page = kPageInit;
    [self getWithdrawRecord:self.page];
}
- (void)getWithdrawRecord:(NSInteger)page
{
    [ServiceManager getUserWithdrawRecordWithPageIndex:page success:^(NSDictionary *data) {
        if (page == kPageInit || self.m_messagesArray == nil)
        {
            self.m_messagesArray = [NSMutableArray array];
        }
        NSArray * datas = data[@"result"];
        if (datas.count>0)
        {
            self.page=page;
            [self.m_messagesArray addObjectsFromArray:datas];
        }
        if (self.m_messagesArray.count ==0)
        {
            self.ui_tableView.hidden = YES;
        }
        else
        {
            self.ui_tableView.hidden = NO;
        }
        [self.ui_tableView reloadData];
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [PublicMethod setFinishRefreshView:self.ui_tableView];
        [self performSelector:@selector(stopAnimate:) withObject:nil afterDelay:2.0];
    } failure:^(NSDictionary *data) {
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [self.ui_tableView.pullToRefreshView stopAnimating];
    }];
}
- (void)stopAnimate:(NSTimer*)timer
{
    [self.ui_tableView.pullToRefreshView stopAnimating];
    [PublicMethod setPullToRefreshView:self.ui_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger section;

    if([self.m_messagesArray isKindOfClass:[NSMutableArray class]])
    {
        section = self.m_messagesArray.count;
    }
    else
    {
        section = 0;
    }

    return section;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger rows = 1;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    
    cell = [self subsidiaryCell:indexPath];
    
    return cell;
}

- (XHSubsidiaryCell *)subsidiaryCell:(NSIndexPath *)indexPath
{
    XHSubsidiaryCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHSubsidiaryCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary * dic = self.m_messagesArray[indexPath.section];
    if ([dic[@"applayType"] isEqualToString:@"alipay"])
    {
        cell.ui_withdrawalType.image = [UIImage imageNamed:@"shouye_icon_zhifubao"];
        cell.ui_withdrawalNumber.text = [NSString stringWithFormat:@"%@",dic[@"transferAccount"]];
    }
    else if ([dic[@"applayType"] isEqualToString:@"bank"])
    {
        cell.ui_withdrawalType.image = [UIImage imageNamed:@"geren_icon_yinhangka"];
        NSString * bankCard = [NSString stringWithFormat:@"%@",dic[@"transferAccount"]];
        cell.ui_withdrawalNumber.text = [NSString stringWithFormat:@"%@**** ****%@",[bankCard substringToIndex:4],[bankCard substringFromIndex:bankCard.length-4]];
    }
    else if ([dic[@"applayType"] isEqualToString:@"wx_pub"])
    {
        cell.ui_withdrawalType.image = [UIImage imageNamed:@"shouye_icon_weixin"];
        cell.ui_withdrawalNumber.text = [NSString stringWithFormat:@"提现昵称:%@",dic[@"weChatNickName"]];
    }
    if ([dic[@"applyStatus"] isEqualToString:@"appling"])
    {
        cell.ui_withdrawalState.text = [NSString stringWithFormat:@"即将到账"];
        cell.ui_withdrawalState.textColor = [ColorManager colorWithHexString:@"#E45252"];
    }
    else if ([dic[@"applyStatus"] isEqualToString:@"success"])
    {
        cell.ui_withdrawalState.text = [NSString stringWithFormat:@"提现成功"];
        cell.ui_withdrawalState.textColor = [ColorManager colorWithHexString:@"#000000" alpha:0.8];
    }
    cell.ui_withdrawalMoney.text = [NSString stringWithFormat:@"￥%.2f",[dic[@"outAmount"] doubleValue]];
    cell.ui_withdrawalOrder.text = [NSString stringWithFormat:@"提现编号:%@",dic[@"applayNo"]];
    cell.ui_yearLabel.text = [[NSDate dateWithString:dic[@"recDate"]] stringWithAllTime];

    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=90;
    
    return height;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    CGFloat height = 10;
    return height;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
